﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using Be;
using WebGui.BllBookingService;
using WebGui.BllLessonService;
using System.Linq;
using System.Web.UI;

namespace WebGui.Admin {

    /// <summary>
    /// The code-behind for the Open.aspx page
    /// </summary>
    public partial class Open : Page {

        /// <summary>
        /// The lesson
        /// </summary>
        private BeLesson _event;

        /// <summary>
        /// The LessonServiceClient
        /// </summary>
        private LessonServiceClient _lesson;

        /// <summary>
        /// The BookingServiceClient
        /// </summary>
        private BookingServiceClient _booking;

        /// <summary>
        /// Loads the page
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            _lesson = (LessonServiceClient) Session["Lesson"];
            _booking = (BookingServiceClient) Session["Booking"];
            if(!IsPostBack) {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                LoadLesson(id);
                Listloader(id);
                TxtStart.Text = _event.StartTime.ToShortTimeString();
                TxtEnd.Text = _event.EndTime.ToShortTimeString();
                TxtComment.Text = _event.Comments;
            }
        }

        /// <summary>
        /// Loads all booking on a lesson
        /// </summary>
        /// <param name="id">The id of the lesson</param>
        private void Listloader(int id) {
            try {
                BeLesson lesson = new BeLesson(id);
                List<BeBooking> bookings = _booking.GetLessonbooked(lesson).ToList();
                string user = "";
                foreach(var beBooking in bookings) {
                    Guid providerId = new Guid(beBooking.UserId);
                    MembershipUser bruger = Membership.GetUser(providerId);
                    user = bruger.UserName;
                    ListBooking.Items.Add(beBooking.StartTime.ToShortTimeString() + " Endtime: " +
                                          beBooking.EndTime.ToShortTimeString() + " User: " + user);
                }
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }

        }

        /// <summary>
        /// Get a lesson
        /// </summary>
        /// <param name="id">The id of the lesson</param>
        private void LoadLesson(int id) {
            try {
                _event = _lesson.GetLesson(id);
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }
        }

        /// <summary>
        /// The close button click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void btnClose_Click(object sender, EventArgs e) {
            Modal.Close(this, "bye");
        }
    }
}